package sketch.instrumenter;

import java.util.List;

import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;

import sketch.util.Checker;

public class SourceCodeInstrumenter {
	
	public static final String INSTRUMENTED = "_instrumented";

	public final CompilationUnit unit;
	
	public final SourceCodeInstrumentingVisitor visitor;
	
	private boolean isInstrumented = false;
	
	public static boolean change_name = true;
	
	public SourceCodeInstrumenter(CompilationUnit unit) {
		this.unit = unit;
		visitor = new SourceCodeInstrumentingVisitor(unit);
	}
	
	public void doInstrumentation() {
		this.unit.accept(visitor);
		this.isInstrumented = true;
		//change the instrumented name
		if(change_name) {
			List<AbstractTypeDeclaration> types = this.unit.types();
		  for(AbstractTypeDeclaration type : types) {
			type.getName().setIdentifier(type.getName().toString() + INSTRUMENTED);
		  }
		}
	}
	
	public CompilationUnit getInstrumentedUnit() {
		if(!isInstrumented) {
			this.doInstrumentation();
		}
		return unit;
	}
	
	public boolean hasInstrumentedTheCode() {
		Checker.checkTrue(this.isInstrumented, "You should call doInstrumentation() first.");
		return visitor.hasInstrumented();
	}
	
	
}